/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: MyLogFilter
 * Author:   Administrator
 * Date:     2019/12/30 23:06
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.lumeng.spring.common.filter;

import com.lumeng.spring.common.handler.HeaderMapRequestWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 功能描述:
 * <>
 *
 * @program: etc_issue
 * @ClassName: MyLogFilter
 * @author: wanghui
 * @date: 2019/12/30 23:06
 * @Version: 1.0.0
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
@ServletComponentScan
@Configuration
@WebFilter(filterName="myLogFilter",urlPatterns="/*")
@Order(value = 1)
public class MyLogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        Long traceId = StringUtils.isNotEmpty(req.getHeader("traceId"))?Long.parseLong(req.getHeader("traceId")):System.currentTimeMillis();
        HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req);
        requestWrapper.addHeader("traceId",traceId);
        chain.doFilter(requestWrapper, response);
    }

    @Override
    public void destroy() {

    }
}
